﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Task;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_TaskManager_AddMessage : System.Web.UI.Page
{
    protected Department objDepartment = new Department();
    MessageManag objMessageManag = new MessageManag();
    MessageUserInfoManag objMessageUserInfoManag = new MessageUserInfoManag();
    ViewQuery objViewQuery = new ViewQuery();
    ArrayList objDepartmentList = new ArrayList();
    string search_AllDepartmentUser = "select * from VIEW_AllUserDepartment ";
    int userCount_Check = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageLoad();
        }
    }

    private void PageLoad()
    {

        //tv_Department.Attributes.Add("onclick", "TreeNodeChecked()"); //注册客户端事件
        DataTable dt = objViewQuery.Query(search_AllDepartmentUser);
        objMessageManag.GetDepartmentUserTree(dt, ref tv_Department);
        

    }

    #region 级联父子节点的复选框状态

    protected void tv_Department_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        SetChildChecked(e.Node);
        //判断是否根节点
        if (e.Node.Parent != null)
        {
            SetParentChecked(e.Node);
        }
    }

    /// <summary>
    /// 根据父节点状态设置子节点的状态
    /// </summary>
    /// <param name="parentNode"></param>
    private void SetChildChecked(TreeNode parentNode)
    {
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            node.Checked = parentNode.Checked;
            if (node.ChildNodes.Count > 0)
            {
                SetChildChecked(node);
            }
        }
    }

    private void SetParentChecked(TreeNode childNode)
    {
        TreeNode parentNode = childNode.Parent;
        if (!parentNode.Checked && childNode.Checked)
        {
            int ichecks = 0;
            foreach (TreeNode node in parentNode.ChildNodes)
            {
                if (node.Checked)
                {
                    ichecks++;
                }
            }
            if (ichecks == parentNode.ChildNodes.Count)
            {
                parentNode.Checked = true;
                if (parentNode.Parent != null)
                {
                    SetParentChecked(parentNode);
                }
            }
        }
        else if (parentNode.Checked && !childNode.Checked)
        {
            parentNode.Checked = false;
        }
    }
    #endregion

   



    protected void btnSave_Click(object sender, EventArgs e)
    {
        Message objMessage = new Message();
        objMessage.MsgTitle = txtTitle.Text;
        objMessage.MsgContent = txt_MsgContent.Text;
        objMessage.MsgCreateTime = DateTime.Now;
        objMessage.MsgType = 1;
        objMessage.FKMsgCreateID = 10000;
        int msgID=objMessageManag.AddNewID(objMessage);
        
        if (msgID != 0)
        {

            for (int i = 0; i < tv_Department.Nodes.Count; i++)
            {
                objMessageUserInfoManag.AddCheckTreeNode(tv_Department.Nodes[i], msgID, ref userCount_Check);

            }
        }

        Script.RedirectURL("MessageManager.aspx");
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EditMessage.aspx");
    }

}
